package com.hyjx.framework.component.task;

import com.hyjx.framework.common.util.StringHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * 任务管理控制器
 */
@Controller
@RequestMapping("/task")
public class TaskController {
    @Resource
    private TaskService service;

    /**
     * 进入到信息查询页面
     *
     * @return
     */
    @RequestMapping(value = "/gotoList", method = RequestMethod.GET)
    public String gotoList(HttpServletRequest request) {
        return "/framework/task/list";
    }

    /**
     * 执行客户查询操作
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "/queryList", method = RequestMethod.POST)
    @ResponseBody
    public String queryList(TaskModel model, HttpServletRequest request) {
        return service.queryList(model);
    }

    /**
     * 执行客户查询操作
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "/queryLogList", method = RequestMethod.POST)
    @ResponseBody
    public String queryLogList(TaskLogModel model, HttpServletRequest request) {
        return service.queryLogList(model.getTaskid());
    }

    /**
     * 跳转到增加页面
     *
     * @return
     */
    @RequestMapping(value = "/gotoAdd", method = RequestMethod.GET)
    public String gotoAdd(HttpServletRequest request) {
        return "/framework/task/add";
    }

    /**
     * 增加信息
     *
     * @param TaskModel
     * @return
     */
    @RequestMapping(value = "/doAdd", method = RequestMethod.POST)
    @ResponseBody
    public String doAdd(TaskModel model) {
        model.setTaskid(StringHelper.getPrimaryKey());
        if (model.getClassname().endsWith(";"))
            model.setClassname(model.getClassname().substring(0, model.getClassname().length() - 1));
        if (service.add(model)) {
            TaskManager.init();
            return "success";
        } else {
            return "failure";
        }
    }

    /**
     * 跳转到修改页面
     * request
     *
     * @return
     */
    @RequestMapping(value = "/gotoEdit", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView gotoEdit(HttpServletRequest request) {
        String id = request.getParameter("id");
        TaskModel model = service.getInfoById(id);
        ModelAndView mav = new ModelAndView("/framework/task/edit");
        mav.addObject("model", model);
        return mav;
    }

    /**
     * 修改信息
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/doEdit", method = RequestMethod.POST)
    @ResponseBody
    public String doEdit(TaskModel model) {
        if (model.getClassname().endsWith(";"))
            model.setClassname(model.getClassname().substring(0, model.getClassname().length() - 1));
        if (service.edit(model) > 0) {
            TaskManager.init();
            return "success";
        } else {
            return "failure";
        }
    }

    /**
     * 删除信息
     * request
     *
     * @return
     */
    @RequestMapping(value = "/gotoDelete", method = RequestMethod.GET)
    @ResponseBody
    public String gotoDelete(HttpServletRequest request) {
        String ids = request.getParameter("ids");
        if (service.delete(ids) > 0) {
            TaskManager.init();
            return "success";
        } else {
            return "failure";
        }
    }

    /**
     * 删除信息
     * request
     *
     * @return
     */
    @RequestMapping(value = "/gotoDeleteLog", method = RequestMethod.GET)
    @ResponseBody
    public String gotoDeleteLog(HttpServletRequest request) {
        String taskid = request.getParameter("taskid");
        if (service.deletelog(taskid) > 0) {
            return "success";
        } else {
            return "failure";
        }
    }

    /**
     * 信息查看
     * request
     *
     * @return
     */
    @RequestMapping(value = "/gotoView", method = RequestMethod.GET)
    public ModelAndView gotoView(HttpServletRequest request) {
        String id = request.getParameter("id");
        TaskLogModel model = new TaskLogModel();
        model.setTaskid(id);
        ModelAndView mav = new ModelAndView("/framework/task/view");
        mav.addObject("model", model);
        return mav;
    }

    /**
     * 开始任务
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/gotoStart", method = RequestMethod.POST)
    @ResponseBody
    public String gotoStart(HttpServletRequest request, HttpServletResponse response) {
        String ids = request.getParameter("ids");
        try {
            response.getWriter().write(service.start(ids));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 停止任务
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/gotoStop", method = RequestMethod.POST)
    @ResponseBody
    public String gotoStop(HttpServletRequest request, HttpServletResponse response) {
        String ids = request.getParameter("ids");
        try {
            response.getWriter().write(service.stop(ids));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}